//
//  AudioVideoPermission.m
//  YuanXinSystemPermissions
//
//  Created by 晏德智 on 2017/2/8.
//  Copyright © 2017年 晏德智. All rights reserved.
//

#import "AudioVideoPermission.h"
#import <AVFoundation/AVFoundation.h>
#import "PermissionType.h"

@implementation AudioVideoPermission

+ (NSString *)getStatus:(NSString *)type
{
    int status = [AVCaptureDevice authorizationStatusForMediaType:[self typeFromString:type]];
    switch (status) {
        case AVAuthorizationStatusAuthorized:
            return RNPStatusAuthorized;
        case AVAuthorizationStatusDenied:
            return RNPStatusDenied;
        case AVAuthorizationStatusRestricted:
            return RNPStatusRestricted;
        default:
            return RNPStatusUndetermined;
    }
}

+ (void)request:(NSString *)type completionHandler:(void (^)(NSString *))completionHandler
{
    [AVCaptureDevice requestAccessForMediaType:[self typeFromString:type]
                             completionHandler:^(BOOL granted) {
                                 dispatch_async(dispatch_get_main_queue(), ^{
                                     completionHandler([AudioVideoPermission getStatus:type]);
                                 });
                             }];
}

+ (NSString *)typeFromString:(NSString *)string {
    if ([string isEqualToString:@"audio"]) {
        return AVMediaTypeAudio;
    } else {
        return AVMediaTypeVideo;
    }
}


@end
